#include "mapwidget.h"
#include "ui_mapwidget.h"

MapWidget::MapWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MapWidget)
  //_trace()
{
    ui->setupUi(this);
    ui->graphicsView->setScene(new QGraphicsScene);
}

MapWidget::~MapWidget()
{
    delete ui;
}

void MapWidget::resizeEvent(QResizeEvent *)
{
    ui->graphicsView->fitInView(ui->graphicsView->sceneRect());//scene()->sceneRect());
    ui->graphicsView->update();
}

void MapWidget::update(GameMap *map, MapMode mode)
{
    //qDebug() << "## MapWidget::update(): Reconstructing map.";
    /*foreach(QGraphicsLineItem* itm, _trace)
        ui->graphicsView->scene()->removeItem(itm);*/
    ui->graphicsView->scene()->clear();

    for(int i=0; i<map->rows(); i++)
    {
        for(int j=0; j<map->cols(); j++)
        {
            Case c = map->at(i, j);
            QColor color;
            QString toolTip;

            if(c.vampires()>0)
            {
                color = Qt::red;
                toolTip += "<b>Vampire Stack</b>\n";
            }
            else if(c.werewolves() > 0)
            {
                color = Qt::blue;
                toolTip += "<b>Werewolves Stack</b>\n";
            }
            else if(c.humans()>0)
            {
                toolTip += "<b>Human House</b>\n";
                if(mode == THREAT)
                {
                    if(c.threatLevel() > Stack::_allies.first()->strenght())
                        color = Qt::darkRed;
                    else
                        color = Qt::yellow;
                }
                else
                    color = Qt::darkYellow;
            }
            else if(mode == THREAT)
            {
                if(c.threatLevel() > 0)
                    color = Qt::darkRed;
                else
                    color = Qt::darkGreen;
            }
            else
                color = Qt::black;

            toolTip += "Strenght:" + QString::number(c.allies()+c.ennemy()+c.humans())
                    +"\nThreat  :" + QString::number(c.threatLevel());
            QBrush brush(color);
            ui->graphicsView->scene()->addRect(j,i,1,1,QPen(Qt::gray),brush)->setToolTip(toolTip);
            //ui->graphicsView->scene()->addText(QString::number(c.allies()+c.ennemy()+c.humans()));
        }
    }

    /*foreach(QGraphicsLineItem* itm, _trace)
        ui->graphicsView->scene()->addItem(itm);
    _trace.clear();*/
    ui->graphicsView->fitInView(ui->graphicsView->sceneRect());//scene()->sceneRect());
    ui->graphicsView->update();
}

void MapWidget::target(int r1, int c1, int r2, int c2, QColor c)
{
    //qDebug() << "## MapWidget::target(): Drawing line...";
    QGraphicsLineItem* line = new QGraphicsLineItem(r1+0.5, c1+0.5,
                                                    r2+0.5, c2+0.5);
    line->setPen(QPen(c));
    //_trace.append(line);
    ui->graphicsView->scene()->addItem(line);
    ui->graphicsView->update();
}
